import {Component, Input, Output, EventEmitter, OnInit, ViewChild} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {ApmService} from '../apm.service';

@Component({
  selector: 'app-apm-update',
  styleUrls: ['./apm-edit.component.css'],
  templateUrl: './apm-edit.component.html',
})
export class ApmEditComponent implements OnInit {
  region: any [] = [];
  // --------------------------------------------------------------------------------------
  @Input() rowData: Element;
  @Output() refreshData = new EventEmitter();
  @ViewChild('resetFormComponent') resetFormComponent;
  isVisible = false;
  validateForm: FormGroup;
  loading = true;
  tourism: Element;
  showModal(): void {
    this.isVisible = true;
  }

  handleOk(): void {
    this.update(this.tourism);
    this.isVisible = false;
  }

  handleCancel(): void {
    this.isVisible = false;
  }

  getInvalid(name) {
    return this.resetFormComponent.getInvalidMessage(name);
  }

  update(data): void {
    this.mainService.update(data.id, data).subscribe(respose => this.refreshData.emit());
  }

  // ---------------------------------------------------------------------------------------
  constructor(private fb: FormBuilder, private mainService: ApmService) {

    this.validateForm = this.fb.group({
      activeAddress: [null, [Validators.required]],
      telephone: [null, [Validators.required]],
    });
  }

  ngOnInit() {
    this.mainService.getByid(this.rowData.id).subscribe(response => {
      this.tourism = response;
    });
  }
}

export class Element {
  id: any;
  activeName: any;
  activeType: any;
  activeAddress: any;
  beginTime: any;
  endTime: any;
  telephone: any;
}
